/*
 * RouteLinkBoard.java
 *
 * Created on March 5, 2007, 3:23 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.routenet;

import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.Timetable;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.transit.*;
import java.awt.Color;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.fpdev.core.basenet.*;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;
import org.fpdev.util.FPUtil;

/**
 *
 * @author demory
 */
public class RLinkBoard extends RLink {

  private RLinkSub rLink_;
  private int dli_;
  private BNode startSNode_; // street node that is the origin of the walk portion
  private List<BLink> accessLinks_;
  private int accessLenFt_;

  /** Creates a new instance of CRouteLinkBoard */
  public RLinkBoard(BNode startSNode, RLinkSub rLink, int dli, List<BLink> accessLinks) {
    rLink_ = rLink;
    dli_ = dli;
    startSNode_ = startSNode;
    accessLinks_ = new LinkedList<BLink>(); //accessLinks;
    Iterator<BLink> links = accessLinks.iterator();
    accessLenFt_ = 0;
    //System.out.println("new bd link "+rLink.getSubRoute().getFullID());
    while (links.hasNext()) {
      BLink link = links.next();
      accessLenFt_ += link.getLengthFeet();
      //if(!link.isStation()) 
      accessLinks_.add(link);
    }
  }

  public Iterator<BLink> getAccessLinks() {
    return accessLinks_.iterator();
  }

  public int getAccessLenFt() {
    //if(rLink_.getSubRoute().getFullID().equals("NE-S:NB")) System.out.println("ret aLF for "+rLink_.getSubRoute().getFullID()+":"+accessLenFt_);
    return accessLenFt_;
  }

  public RNode getEndRouteNode() {
    return rLink_.getToNode();
  }

  public BNode getWalkStart() {
    return startSNode_;
  }

  public int getType() {
    return RLINK_BOARD;
  }

  public String getID() {
    return "bd_" + rLink_.getID() + "_" + dli_;
  }

  public SubRoute getSubRoute() {
    return rLink_.getSubRoute();
  }

  public int getStartDLI() {
    return dli_;
  }

  public int getEndDLI() {
    return rLink_.getEndDLI();
  }

  public int timeCostFW(int inTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    boolean dbg = false; // rLink_.getSubRoute().getFullID().equals("NE-S:NB");
    if (!rLink_.getSubRoute().hasTable(service)) {
      return 100000000;
    }
    if (dbg) {
      System.out.println("   tcFW/bd " + rLink_.getSubRoute().getFullID() + " @ " + inTime + "/" + FPUtil.sTimeToStr(inTime) + " dli " + dli_);
    }
    double secs = (double) accessLenFt_ / walkSpeedFPS;
    if (dbg) {
      System.out.println("   walk secs: " + secs);
    }
    int arrTime = inTime + (int) secs;
    if (dbg) {
      System.out.println("   arrTime: " + arrTime + "/" + FPUtil.sTimeToStr(arrTime));
    }
    int run = rLink_.getSubRoute().getNextRun(service, arrTime + transferTime, dli_, dbg);
    if (run == 0) {
      return 100000000;
    }
    int outTime = rLink_.getSubRoute().getTime(service, run, rLink_.getEndDLI() + 1);
    lastRun_ = run;
    return (outTime - inTime);
  }

  public int timeCostBW(int outTime, CalendarService service, double walkSpeedFPS, int transferTime) {
    if (!rLink_.getSubRoute().hasTable(service)) {
      return 100000000;
    }
    //System.out.println("   tcBW/bd "+getID()+" @ "+CUtil.sTimeToStr(outTime));
    //System.out.println("   acclenft: "+this.getAccessLenFt());
    double secs = (double) accessLenFt_ / walkSpeedFPS;
    //int arrTime = inTime + (int) secs;
    int run = rLink_.getSubRoute().getPrevRun(service, outTime - transferTime, rLink_.getEndDLI());
    if (run == 0) {
      return 100000000;
    }
    int inTime = rLink_.getSubRoute().getTime(service, run, dli_);
    lastRun_ = run;
    int total = (outTime - inTime) + (int) secs;
    //System.out.println("   onroute: "+(outTime - inTime)+"/"+CUtil.elapsedTimeStr(outTime - inTime));    
    //System.out.println("   access: "+(int)secs+"/"+CUtil.elapsedTimeStr((int)secs));
    //System.out.println("   total: "+total+"/"+CUtil.elapsedTimeStr(total));
    return total;
  }

  public void draw(MapCanvas canvas) {
    canvas.setColor(Color.orange);
    canvas.drawLine(startSNode_.getX(), startSNode_.getY(), rLink_.getToNode().getX(), rLink_.getToNode().getY(), true);

  }
}
